package com.food.user.controller;


import cn.hutool.core.io.FileUtil;
import com.food.user.commos.Result;
import com.food.user.dto.CommentInfoDto;
import com.food.user.entity.CommentInfo;
import com.food.user.exception.MyParameterException;
import com.food.user.form.CommentOrderForm;
import com.food.user.form.UserQueryCommentForm;
import com.food.user.service.CommentInfoService;
import com.food.user.util.OSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 评价信息表 前端控制器
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月17日
 */
@RestController
@RequestMapping("/commentInfo")
@Api(tags = "用户评论接口")
public class CommentInfoController {

    @Autowired
    private CommentInfoService commentInfoService;
    @Autowired
    private OSSUtil ossUtil;

    @PostMapping("/commentOrder")
    @ApiOperation("评价订单")
    public Result commentOrder(@RequestBody CommentOrderForm form, BindingResult br)throws Exception{
        if (br.hasErrors()) {
            List<ObjectError> allErrors = br.getAllErrors();
            for (ObjectError allError : allErrors) {
                //获取打印提示信息
                System.out.println(allError.getDefaultMessage());
            }
            throw new MyParameterException(500, "参数校验失败");
        }
        CommentInfo commentInfo = commentInfoService.commentOrder(form);
        return Result.success(200,"评价订单成功",commentInfo);
    }

    @GetMapping("/query")
    @ApiOperation("查询店铺全部评论")
    public Result queryCommentByOrderNum(Long storeId)throws Exception{
        List<CommentInfo>  commentInfoList=commentInfoService.queryCommentByOrderNum(storeId);
        return Result.success(200,"查看评论成功",commentInfoList);
    }

    @PostMapping("/upload")
    @ApiOperation("上传评论图片")
    public Result uploadFoodImage(MultipartFile file) throws Exception{
        //MultipartFile重要API
        InputStream is = file.getInputStream();//获取文件内容
        String filename = file.getOriginalFilename();//获取文件名称
        //取出后缀名
        String suffix = FileUtil.getSuffix(filename);
        filename= UUID.randomUUID().toString().replaceAll("-","");
        String url = ossUtil.uploadFile(is, filename + "." + suffix, "woniu-food-tyy");
        return Result.success(200,"上传图片成功",url);
    }

}

